package co.yixiang.yshop.framework.weixin.config;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 微信小程序配置文件
 *
 * @author: moxiangrong
 **/
@Configuration
@ConditionalOnClass(WxMaService.class)
@EnableConfigurationProperties(WxMiniProgramProperties.class)
public class WxMiniProgramConfig {

    /*注入小程序相关配置*/
    @Autowired
    private WxMiniProgramProperties properties;

    /**
     * 配置默认参数
     */
    @Bean
    @ConditionalOnMissingBean
    public WxMaConfig wxMaConfig() {
        WxMaDefaultConfigImpl wxMaDefaultConfig = new WxMaDefaultConfigImpl();
        //设置默认参数-appid,secret
        wxMaDefaultConfig.setAppid(this.properties.getAppid());
        wxMaDefaultConfig.setSecret(this.properties.getSecret());
        return wxMaDefaultConfig;
    }

    /**
     * 配置WxMaService
     */
    @Bean
    @ConditionalOnMissingBean
    public WxMaService wxMaService(WxMaConfig wxMaConfig) {
        WxMaService wxMaService = new WxMaServiceImpl();
        wxMaService.setWxMaConfig(wxMaConfig);
        return wxMaService;
    }


}
